package com.five.system.controller;

import com.five.basic.result.PageList;
import com.five.pojo.Menu;
import com.five.result.Result;
import com.five.system.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user/menu")
@Slf4j
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 分页查询
     * @param menu
     * @return
     */
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody Menu menu) {
        try {
            PageList<Menu> menuPageList = menuService.queryPage(menu);
            return Result.success(menuPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        try {
            menuService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        try {
            menuService.deleteBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    /**
     * 查询所有父菜单
     * @return
     */
    @PostMapping("/menuTree")
    public Result queryMenuTree() {
        try {
            List<Menu> menuList = menuService.queryMenuTree();
            return Result.success(menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }


    /**
     * 查询父菜单数量
     */
    @PostMapping("/queryCountByParent")
    public Result queryCountByParent() {
        try {
            Integer count = menuService.queryCountByParent();
            return Result.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 根据父级id查询子菜单数量
     * @param parentId
     * @return
     */
    @PostMapping("/queryCountByParentId/{parentId}")
    public Result queryCountByParentId(@PathVariable("parentId") Long parentId) {
        try {
            Integer count = menuService.queryCountByParentId(parentId);
            return Result.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    /**
     * 保存更新菜单
     * @param menu
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        log.info("保存菜单信息：{}", menu);
        try {
            if (menu.getId() != null) {
                menuService.update(menu);
                return Result.success("更新成功");
            } else {
                menuService.add(menu);
                return Result.success("保存成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询菜单树
     * @return
     */
    @PostMapping("/tree")
    public Result tree() {
        try {
            List<Menu> listTree = menuService.queryTree();
            return Result.success(listTree);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常");
        }
    }

    /**
     * 根据角色id查询菜单(id)
     * @param id
     * @return
     */
    @PostMapping("/getOwnMenus/{id}")
    public Result getOwnMenus(@PathVariable("id") Long id) {
        try {
            List<Long> ids = menuService.queryMenusView(id);
            return Result.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常");
        }
    }

    @PostMapping("queryByUserId/{userId}")
    public Result<List<Menu>> queryMenusByUserId(@PathVariable Long userId){
        try {
            List<Menu> menus = menuService.queryByUserId(userId);
            return Result.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务器异常");
        }
    }



}
